<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Models\Admin;
use App\Http\Controllers\Controller;
use Cookie;
use DB;
use Illuminate\Support\Facades\Validator;

class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        //验证登录信息
        $data = $request->all();
        $res = DB::table('admins')->where('a_uname',$data['username'])->get();
        if(!$res){
            echo "<script>alert('用户名错误');</script>";
            return view('admin.lei.login');
        }
        $res1 = DB::table('admins')->where('a_uname',$data['username'])->where('a_upass',$data['password'])->get();
        if(!$res1){
            echo "<script>alert('密码错误');</script>";
            return view('admin.lei.login');
        }
        if($res1[0]['a_status']!=0){
            echo "<script>alert('权限不够');</script>";
            return view('admin.lei.login');
        }
        $code = session('code');
        $code1 = $data['code'];
        if($code != $code1){
            echo "<script>alert('验证码错误');</script>";
            return view('admin.lei.login');
        }

        //判断是否记住用户名
        if($request->input('remember')){
            Cookie::queue('a_uname',$data['username'],60*24*7);
        }
        // dd($res1);
            session(['info'=>$res1[0]]);
            return redirect('/t_admin');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Admin::find($id);
        return view('admin.lei.index.edit',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token','_method');
        $rule = [
            'newpass'=>'required|between:5,18',
            'renewpass'=>'required|between:5,18',
        ];
        $mess = [
            'newpass.required'=>'密码必须输入',
            'newpass.between'=>'密码的长度在5-18位之间',
            'renewpass.required'=>'密码必须输入',
            'renewpass.between'=>'密码的长度在5-18位之间',
        ];
        $validator = Validator::make($data,$rule,$mess);

        if($validator->fails()){
            return back()
                ->withErrors($validator)
                ->withInput();
        }
        if($data['oldpass'] == $data['newpass']){
            return back()->with('errors','新密码不能与原密码相同');
        }
        if($data['newpass'] != $data['renewpass']){
            return back()->with('errors','俩次输入密码不相同');
        }
        $user = Admin::find($id);
        $user->a_upass = $data['newpass'];
        $res = $user->save();
        if($res){
            return redirect('/t_admin')->with('success','密码修改成功');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
